package org.springblade.tms.module.billing.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.logging.log4j.util.Strings;
import org.springblade.common.utils.EnumUtils;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.tms.global.enums.TankTypeEnum;
import org.springblade.tms.global.enums.TorTypeEnum;
import org.springblade.tms.global.enums.WaybillStatusEnum;
import org.springblade.tms.module.billing.base.SettlementService;
import org.springblade.tms.module.billing.dto.TMBillingDTO;
import org.springblade.tms.module.billing.vo.TMBillingVO;
import org.springblade.tms.module.billingitem.dto.TMBillingItemDTO;
import org.springblade.tms.module.billingitem.service.IBillingItemService;
import org.springblade.tms.module.billingitem.vo.TMBillingItemVO;
import org.springblade.tms.module.currencytype.entity.TMCurrencyType;
import org.springblade.tms.module.currencytype.service.ICurrencyTypeService;
import org.springblade.tms.module.statementorder.dto.SettlementSheetDTO;
import org.springblade.tms.module.statementorder.vo.TMStatementExcelOrderVO;
import org.springblade.tms.module.transportorder.dto.TransportOrderDTO;
import org.springblade.tms.module.transportorder.vo.TMTransportOrderVO;
import org.springblade.tms.module.transportorder.vo.TransportOrderExcel;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * @projectName: BladeX
 * @package: org.springblade.tms.module.billing.controller
 * @className: SettlementController
 * @author: yr
 * @description: 结算单发票
 * @date: 2022/4/20 10:11
 */
@RestController
@RequestMapping("billing/Settlement")
@Api(value = "结算单发票-yr", tags = "结算单发票-yr")
public class SettlementController {
	@Resource
	private SettlementService settlementService;
	@Resource
	private ICurrencyTypeService currencyTypeService;
	@Resource
	private IBillingItemService billingItemService;

	/**
	 * 分页 结算单
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "结算单查询", notes = "传入billing")
	public R<IPage<TMBillingVO>> list(TMBillingDTO tmBilling, Query query) {
		return settlementService.list(tmBilling, query);
	}

	/**
	 * 分页 结算单
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "发票查询", notes = "传入结算单号")
	public R<IPage<TMBillingItemVO>> detail(@RequestParam String no, Query query) {
		return settlementService.detail(no, query);
	}

	/**
	 * 币别列表
	 */
	@GetMapping("/currency")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "币别列表")
	public List currency() {
		List<TMCurrencyType> list = currencyTypeService.lambdaQuery().list();
		return list;
	}

	/**
	 * 新增或修改 发票明细
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增或修改", notes = "传入billingItem")
	public R<Boolean> submit(@Valid @RequestBody TMBillingItemDTO billingItemDTO) {
		return R.status(settlementService.saveOrUpdate(billingItemDTO));
	}

	/**
	 * 删除 发票明细
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "删除发票", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(billingItemService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 提交发票明细
	 */
	@PostMapping("/refer")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "提交", notes = "传入结算单号")
	public R<Boolean> refer(@Valid @RequestParam String no) {
		return R.status(settlementService.refer(no));
	}


	/**
	 * 删除结算单
	 */
	@GetMapping("/deleteStatement")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "删除结算单", notes = "传入ids")
	public R deleteStatement(@ApiParam(value = "主键", required = true) @RequestParam Long id) {
		return settlementService.deleteStatement(id);
	}

	/**
	 * 结算单发票明细
	 */
	@GetMapping("/statementInvoiceDetails")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "结算单发票明细", notes = "传入结算单号")
	public R statementInvoiceDetails(SettlementSheetDTO settlementSheetDTO, Query query) {
		return settlementService.statementInvoiceDetails(settlementSheetDTO, query);
	}

	/**
	 * @USER: YYJ
	 * @DATE: 2022/12/20 11:19
	 * @DESCRIPTION: 结算单发票导出
	 */
	@GetMapping("/download")
	@ApiOperation(value = "结算单导出", notes = "传入transportOrder")
	public void download(SettlementSheetDTO settlementSheetDTO, HttpServletResponse response) {
		List<TMStatementExcelOrderVO> tmStatementExcelOrderVOS = settlementService.download(settlementSheetDTO);
		ExcelUtil.export(response, "结算单发票", "结算单发票", tmStatementExcelOrderVOS, TMStatementExcelOrderVO.class);
	}

}
